/**
 * 
 */
package org.vsg.stock.dzh;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author vison
 *
 */
public class DataConverter {

	private static DecimalFormat decFormat = new DecimalFormat("##0.00");	
	
	public static String convert(List<String> lines , int pos , String line) {
		
		if (pos+1 == lines.size() ) {
			return line+",0";
		}
	
		String[] sprdata = line.split(",");
		double open = new Double(sprdata[2]);
		double close = new Double(sprdata[5]);
		double yieldRate = new Double(sprdata[8]);
		
		String lastDailyDataLine = lines.get(pos+1);
		sprdata = lastDailyDataLine.split(",");
		double lastClose = new Double(sprdata[5]);
		
		double todayRealOpen = close / (1 + yieldRate);
		
		double openRate = (open - lastClose) / lastClose ; 

		StringBuilder outputContent = new StringBuilder(line);
		// --- set ex right is true
		double trLast = Double.parseDouble( decFormat.format( todayRealOpen ) );
		
		if (lastClose == trLast
			|| 	(lastClose+0.01 > trLast && lastClose-0.01 < trLast )
		) {
			outputContent.append(",1");
		} else {
			outputContent.append(",0");
		}

		try {
			return outputContent.toString();
		} finally {
			outputContent = null;
		}
		
	}
	

}
